<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="icon" th:href="@{/img/logo.ico}" href="../static/img/logo.ico" type="images/x-ico"/>

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="../static/css/bootstrap.min.css" th:href="@{/css/bootstrap.min.css}">
    <title>个人信息</title>
    <link rel="stylesheet" th:href="@{/fontawesome/css/font-awesome.min.css}"
          href="../../static/fontawesome/css/font-awesome.min.css">

    <link rel="stylesheet"
          href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,500;0,600;0,700;1,400&display=swap">

    <!-- Fontawesome CSS -->
    <link rel="stylesheet" href="../static/fontawesome/css/fontawesome.min.css"
          th:href="@{/fontawesome/css/fontawesome.min.css}">
    <link rel="stylesheet" href="../static/fontawesome/css/all.min.css" th:href="@{/fontawesome/css/all.min.css}">

    <!-- Main CSS -->
    <link rel="stylesheet" href="../static/css/back_index.css" th:href="@{/css/back_index.css}">
    <style type="text/css">
        table {
            table-layout: fixed;
        }

        td {
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
        }

        ::-webkit-input-placeholder { /* WebKit, Blink, Edge */
            color: darkgrey;
            font-size: small;
        }
    </style>
</head>
<body>
<div class="main-wrapper">
    <div th:replace="commons/bar::#teachermenubar(activeUri1='个人信息')"></div>
    <div class="page-wrapper">
        <div class="content container-fluid">
            <div class="form-group">
                <label for="teacherName"><span style="color: red">*</span>姓名</label>
                <input type="text" class="form-control" id="teacherName" disabled>
            </div>
            <div class="form-group">
                <label for="teacherId"><span style="color: red">*</span>工号</label>
                <input type="text" class="form-control" id="teacherId" disabled>
            </div>
            <div class="form-group">
                <label for="teachcourseId"><span style="color: red">*</span>教授课程</label>
                <select class="form-control" id="teachcourseId" disabled></select>
            </div>
            <div class="form-group">
                <label>性别</label>
                <div class="form-check">
                    <input class="form-check-input" type="radio" name="sex" id="boy" value="男" checked>
                    <label class="form-check-label" for="boy">
                        男
                    </label>
                </div>
                <div class="form-check">
                    <input class="form-check-input" type="radio" name="sex" id="girl" value="女">
                    <label class="form-check-label" for="girl">
                        女
                    </label>
                </div>
                <div class="form-check">
                    <input class="form-check-input" type="radio" name="sex" id="null" value="其他" disabled>
                    <label class="form-check-label" for="null">
                        其他
                    </label>
                </div>
            </div>
            <div class="form-group">
                <label for="workDate">入职时间</label>
                <input type="date" class="form-control" id="workDate">
            </div>
            <div class="form-group">
                <label for="telephone">手机号</label>
                <input type="text" class="form-control" id="telephone">
            </div>
            <div class="form-group">
                <label for="email">常用邮箱</label>
                <input type="text" class="form-control" id="email">
            </div>
            <div class="form-group">
                <label for="address">家庭地址</label>
                <textarea class="form-control" id="address" rows="3"></textarea>
            </div>
            <button type="button" id="submitUpdate" class="btn btn-primary btn-lg btn-block">保存修改</button>
            <button type="button" data-toggle="modal" data-target="#updatePasswordModal"
                    class="btn btn-success btn-lg btn-block">修改密码
            </button>
            <div class="modal fade" id="updatePasswordModal" data-backdrop="static" data-keyboard="false"
                 tabindex="-1" role="dialog"
                 aria-labelledby="updatePasswordLabel" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="updatePasswordLabel">修改密码<label
                                    style="font-size: small">(如忘记密码，请联系管理员重置)</label>
                            </h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body">
                            <p>
                                <label style="font-size: small; font-weight: bold">当前密码</label><br/>
                                <input type="password" placeholder="密码由英文字母和数字组成的4-10位字符"
                                       style="width: 100%" name="current_password" id="current_password"><br/>
                            </p>
                            <p>
                                <label style="font-size: small; font-weight: bold">新密码</label><br/>
                                <input type="password" placeholder="新密码和当前密码不能相同" style="width: 100%"
                                       name="password"
                                       id="new_password">
                                <br/>
                            </p>
                            <p>
                                <label style="font-size: small; font-weight: bold">确认密码</label><br/>
                                <input type="password" placeholder="确认密码和新密码保持一致"
                                       style="width: 100%" id="new_password1"><br/>
                            </p>
                            <span id="mytip" style="color: red; font-size: medium"></span>
                        </div>
                        <div class="modal-footer">
                            <button type="button" id="submitUpdatePassword" class="btn btn-success">修改密码</button>
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                        </div>
                    </div>
                </div>
            </div>
            <footer>
                <p>2020 &copy; 软件学院学生信息管理系统 by 郑焙元</p>
            </footer>
        </div>
    </div>
</div>
<script src="../static/js/jquery-3.5.1.min.js" th:src="@{/js/jquery-3.5.1.min.js}"></script>
<script>
    var password;

    $(function () {
        sendAjax('/teacher/api', 'get', {
            teacherid: [[${session.LoginUser}]],
        }, function (res) {
            password = res.data.password;
            $("#teacherName").val(res.data.teachername);
            $("#teacherId").val(res.data.teacherid);
            sendAjax("/teacher/api/course/" + res.data.teacherid, "get", {}, function (res) {
                var html = '<option value="' + res.data.courseid + '" selected="selected">' + res.data.coursename + '</option>';
                $("#teachcourseId").html(html);
            }, "");
            $("[name = 'sex']").each(function () {
                if ($(this).val() === res.data.sex) {
                    $(this).prop("checked", "checked");
                }
            });
            $("#workDate").val(res.data.workdate === null ? "1970-01-01" : res.data.workdate.substring(0, 10));
            $("#telephone").val(res.data.telephone);
            $("#email").val(res.data.email);
            $("#address").text(res.data.address);
        }, '');
        var buttonGroup = new ButtonGroup();
        buttonGroup.init();
    });

    var ButtonGroup = function () {
        var object = {};

        object.init = function () {
            $("#submitUpdate").click(function () {
                sendAjax('/teacher/api', 'put', {
                    teachername: $("#teacherName").val(),
                    teacherid: $("#teacherId").val(),
                    sex: $("[name = 'sex']:checked").val(),
                    workdate: new Date($("#workDate").val()),
                    telephone: $("#telephone").val(),
                    email: $("#email").val(),
                    address: $("#address").val(),
                }, function (res) {
                    alert("修改成功！");
                    $('#updateTeacherModal').modal('hide');
                    myrefresh();
                }, "#submitUpdate");
            });

            $("#submitUpdatePassword").click(function () {
                alert(password)
                if (password === $("#current_password").val()) {
                    if ($("#new_password").val() !== $("#new_password1").val()) {
                        $("#mytip").text("两次新密码输入不同");
                    } else {
                        if ($("#new_password").val() === "") {
                            $("#mytip").text("新密码输入不能为空");
                        } else {
                            sendAjax("/teacher/api/" + [[${session.LoginUser}]], "post", {
                                newpassword: $("#new_password").val(),
                            }, function (res) {
                                alert("修改成功")
                            });
                            $("#updatePasswordModal").modal("hide");
                            $("#mytip").text("");
                        }
                    }
                } else {
                    $("#mytip").text("当前密码输入有误");
                }
            });
        };

        return object;
    };
</script>
<script src="../static/js/popper.min.js" th:src="@{/js/popper.min.js}"></script>
<script src="../static/js/bootstrap.min.js" th:src="@{/js/bootstrap.min.js}"></script>
<script src="../static/js/jquery.slimscroll.min.js" th:src="@{/js/jquery.slimscroll.min.js}"></script>
<script src="../static/js/script.js" th:src="@{/js/script.js}"></script>

</body>
</html>
